import Taro from "@tarojs/taro";
import { login } from "../service";
import { STORAGE_DATA } from "../utils/constant";

export default {
  namespace: "index",
  state: {
    userInfo: null,
    data: [1, 2, 3]
  },

  effects: {
    *login({ payload, callback }, { call, put }) {
      const res = yield call(login, payload);
      if (res && res.result) {
        let { token, privateKey, id } = res.result;
        Taro.setStorageSync(STORAGE_DATA.TOKEN, token);
        Taro.setStorageSync(STORAGE_DATA.PRIVATEKEY, privateKey);
        Taro.setStorageSync(STORAGE_DATA.USERID, id);
        // Taro.setStorageSync(STORAGE_DATA.USERINFO, JSON.stringify(res.result || {}));
        yield put({
          type: "updateStatus",
          payload: {
            userInfo: {
              ...res.result
            }
          }
        });

        callback(res.result);
      }
    }
  },

  reducers: {
    updateStatus(state, { payload }) {
      return { ...state, ...payload };
    }
  }
};
